package cn.knightzz.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.log.Log;
import cn.knightzz.entity.JsonResult;
import cn.knightzz.entity.SysEvent;
import cn.knightzz.entity.SysFile;
import cn.knightzz.service.SysEventService;
import cn.knightzz.vo.EventCountVO;
import cn.knightzz.vo.PutEventVO;
import cn.knightzz.vo.UpdateEventVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author knightzz
 * @since 2021-05-01
 */
@RestController
public class SysEventController {

    @Resource
    SysEventService eventService;

    @PostMapping("/events")
    public JsonResult<Object> addEvent(@RequestBody PutEventVO eventVO) {
        System.out.println(eventVO);
        eventService.addEvent(eventVO);
        JsonResult<Object> result = new JsonResult<>("200", "事件上传成功!");
        return result;
    }

    @GetMapping("/events")
    public JsonResult<HashMap<String, Object>> selectEventPage(@RequestParam("pagenum") Long pageNum,
                                                               @RequestParam("pagesize") Long pageSize,
                                                               @RequestParam("query") String query) {
        JsonResult<HashMap<String, Object>> result;
        IPage<SysEvent> sysEventIPage = eventService.selectEventPage(pageNum, pageSize, query);
        if (ObjectUtil.isNotNull(sysEventIPage)) {
            // 获取数据总条数
            long total = sysEventIPage.getTotal();
            // 获取当前页码
            long current = sysEventIPage.getCurrent();
            // 获取记录
            List<SysEvent> records = sysEventIPage.getRecords();
            // 封装响应数据
            result = new JsonResult<>("200", "分页数据获取成功");
            HashMap<String, Object> data = new HashMap<>();
            data.put("total", total);
            data.put("pagenum", current);
            data.put("events", records);
            result.setData(data);
        } else {
            result = new JsonResult<>("201", "分页数据获取失败");
        }
        return result;
    }

    @PutMapping("/events")
    public JsonResult<Object> updateEvent(@RequestBody UpdateEventVO eventVO) {
        Boolean status = eventService.updateEvent(eventVO);
        JsonResult<Object> result;
        if (status) {
            result = new JsonResult<>("200", "事件信息更新成功");
        } else {
            result = new JsonResult<>("201", "事件信息更新失败");
        }
        return result;
    }

    @DeleteMapping("/events/{id}")
    public JsonResult<Object> deleteEvent(@PathVariable Long id, @RequestParam("fileList") String fileList) {
        Boolean status = eventService.deleteEventInfo(id, fileList);
        JsonResult<Object> result;
        if (status) {
            result = new JsonResult<>("200", "事件删除成功");
        } else {
            result = new JsonResult<>("201", "事件删除失败");
        }
        return result;
    }

    @GetMapping("/events/infos")
    public JsonResult<List<SysEvent>> getEventInfos(@RequestParam("userId") Long userId,
                                                    @RequestParam("title") String title,
                                                    @RequestParam("startDate") String startDate,
                                                    @RequestParam("endDate") String endDate) {
        List<String> dates = new ArrayList<>();
        if (!startDate.equals("")){
            dates.add(startDate);
            dates.add(endDate);
        }
        List<SysEvent> data = eventService.getEventInfosByUser(userId, title,dates);
        JsonResult<List<SysEvent>> result = new JsonResult<>("200", "事件信息获取成功!");
        result.setData(data);
        return result;
    }

    @GetMapping("/events/author/{id}")
    public JsonResult<List<SysEvent>> getEventListByAuthorId(@PathVariable("id") Long authorId){

        List<SysEvent> eventList = eventService.getEventByAuthorId(authorId);

        JsonResult<List<SysEvent>> result = new JsonResult<>("200" , "查询成功!");
        result.setData(eventList);
        return result;
    }

    @GetMapping("/events/info/{id}")
    public JsonResult<SysEvent> getEventById(@PathVariable("id") Long eventId){

        SysEvent eventById = eventService.getEventById(eventId);

        JsonResult<SysEvent> result = new JsonResult<>("200" , "查询成功!");
        result.setData(eventById);
        return result;
    }

    @GetMapping("/events/user/{userId}/status/{status}")
    public JsonResult<List<SysEvent>> getEventByStatus(@PathVariable("userId") Long userId,
                                                 @PathVariable("status") Integer status, @RequestParam String query){

        List<SysEvent> eventByStatus = eventService.getEventByStatus(userId, status, query);

        JsonResult<List<SysEvent>> result = new JsonResult<>("200" , "查询成功!");
        result.setData(eventByStatus);
        return result;
    }

    @GetMapping("/events/count")
    public JsonResult<HashMap<String,Object>> selectCountVO(@RequestParam("pageNum") Long pageNum,
                                                        @RequestParam("pageSize") Long pageSize,
                                                        @RequestParam("userId") Long userId,
                                                        @RequestParam("query") String query){

        IPage<EventCountVO> pages = eventService.selectCountEvent(pageNum, pageSize, query, userId);
        JsonResult<HashMap<String,Object>> result = null;
        if(ObjectUtil.isNotNull(pages)){
            long total = pages.getTotal();
            List<EventCountVO> records = pages.getRecords();
            HashMap<String,Object> data = new HashMap<>();
            data.put("total", total);
            data.put("records", records);
            result = new JsonResult<>("200" , "查询成功!");
            result.setData(data);
            return result;
        }else{
            return new JsonResult<>("201", "查询失败!");
        }
    }

}

